import goodsApi from '@/api/shop/goods'
import { getApiUrl } from '@/utils/utils'
import permission from '@/directive/permission/index.js'
import settlementApi from '@/api/settlement/settlement'
export default {
  directives: { permission },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        name: "",
        goodsCreat: 0,
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      apiUrl: getApiUrl(),
      formVisible: false,
      formTitle: '更新库存',
      form: {
        id: 0,
        stock: 0,
      },
      goodsCreatList: [],
      goodsCreatSelect: 0,
      initFlag: true,
    }
  },
  watch: {
    goodsCreatSelect(val, oldVal) {
      console.log('筛选框值变动', val, oldVal);
      this.listQuery.goodsCreat = val
      this.fetchData()
    },
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getGoodsCreatList()
      // this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      goodsApi.getList(this.listQuery).then(response => {
        //console.log('列表数据：', response.data.records);
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    reset() {
      this.listQuery.name = ''
      this.listQuery.page = 1
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    // 新增
    add() {
      this.$router.push('goodsEdit')
    },
    // 编辑
    edit(id) {
      this.$router.push({ path: 'goodsEdit', query: { id: id } })
    },
    changeIsOnSale(data) {
      goodsApi.changeIsOnSale(data.id, data.isOnSale).then(response => {
        this.$message({
          message: data.isOnSale ? '上架成功' : '下架成功',
          type: 'success'
        })
      })

    },
    updateStockForm(param) {
      this.form = {
        id: param.id,
        stock: param.stock,
      }
    },
    updateStock(param) {
      this.updateStockForm(param)
      this.formTitle = '更新库存'
      this.formVisible = true
    },
    updateStockSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          goodsApi.apiUpdateStockSave({
            id: this.form.id,
            stock: this.form.stock,
          }).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
            this.formVisible = false
          })
        } else {
          return false
        }
      })
    },
    copyId(param) {
      param = "{\"id\":" + param + "}"
      //console.log(param);
      if (window.clipboardData) {
        window.clipboardData.setData('text', param);
      } else {
        (function (param) {
          document.oncopy = function (e) {
            e.clipboardData.setData('text', param);
            e.preventDefault();
            document.oncopy = null;
          }
        })(param);
        document.execCommand('Copy');
        this.$message({
          message: '参数' + param + '复制成功',
          type: 'success'
        });
      }
    },
    getGoodsCreatList() {
      settlementApi.getSysUser().then(response => {
        let a = {
          id: 0,
          nameAndAccount: "全部"
        }
        this.goodsCreatList.push(a)
        //console.log("获取创建者ID", response);
        for (let i = 0; i < response.data.length; i++) {
          const e = response.data[i];
          let tmp = {
            id: e.id,
            nameAndAccount: e.nameAndAccount,
            name: e.name,
          }
          this.goodsCreatList.push(tmp)
        }

        this.goodsCreatSelect = 0
        if (this.goodsCreatSelect == 0 && this.initFlag) {
          let thisUserId = this.$store.state.user.profile.id
          for (let i = 0; i < this.goodsCreatList.length; i++) {
            const e = this.goodsCreatList[i];
            if (e.id == thisUserId) {
              this.goodsCreatSelect = e.id
              this.listQuery.goodsCreat = e.id
              break
            } else {
              this.listQuery.goodsCreat = 0
            }
          }
          if (this.listQuery.goodsCreat == 0){
            console.log('goodsCreat = 0')
            this.fetchData()
          }
          this.initFlag = false
        }

      })
    },
  }
}
